package com.ebelter.btcomlib.models.beans;


import android.graphics.Bitmap;

import com.ebelter.btcomlib.models.bluetooth.products.dianji8.Dj8Constant;
import com.ebelter.btcomlib.utils.BytesUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;



/**
 * 秤用户信息
 */
public class ScaleUser {


    public static String currentConnectScaleBlueName;//当前连接的体脂秤的蓝牙名字

    private final String TAG = "ScaleUser";

    private static ScaleUser mUser;

    private String mac;         //---X
    /*255,为华为运动健康用户*/
    private int uid = -1;
    private int age;
    private String birthday;
    private int height;

    private float weight = 0;
    private int sex = 0;//0男1女
    private Bitmap icon;
    private String targetTime;
    private float target = 0.0f;

    private String registTime;
    private float impedance;
    private String huId;
    private String btId;
    private String imgUrl;


    private boolean isKgModel;

    public boolean isKgModel() {
        return isKgModel;
    }

    public void setKgModel(boolean kgModel) {
        isKgModel = kgModel;
    }

    /**
     * 0:普通用户
     * 1:专业运动员
     * 2:业余运动员
     */
    private int roleType;

    public int getRoleType() {
        return roleType;
    }

    public void setRoleType(int roleType) {
        this.roleType = roleType;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    public String getBtId() {
        return btId;
    }

    public String getHuId() {
        return huId;
    }

    public void setHuId(String huId) {
        this.huId = huId;
    }

    public String getRegistTime() {
        return registTime;
    }

    public void setRegistTime(String registTime) {
        this.registTime = registTime;
    }

    public void setBtId(String btId) {
        this.btId = btId;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }


    public String getTargetTime() {
        return targetTime;
    }

    public void setTargetTime(String targetTime) {
        this.targetTime = targetTime;
    }

    public float getTarget() {
        return target;
    }

    public void setTarget(float target) {
        this.target = target;
    }

    public Bitmap getIcon() {
        return icon;
    }

    public void setIcon(Bitmap icon) {
        this.icon = icon;
    }

    public String getNike() {
        return nike;
    }

    public void setNike(String nike) {
        this.nike = nike;
    }

    private String nike;


    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public float getWeight() {
        return weight;
    }

    public void setWeight(float weight) {
        this.weight = weight;
    }

    public float getImpedance() {
        return impedance;
    }

    public void setImpedance(float impedance) {
        this.impedance = impedance;
    }

    public byte[] buildScaleUserData() {
        String[] btIdArr = null;
        if (btId != null) {
            btIdArr = btId.split(":");
        }
        if (btIdArr == null || btIdArr.length != 7) {
            LogUtils.e(TAG, "mac is error");
            btIdArr = new String[]{"0", "0", "0", "0", "0", "0", "0"};
        }

        int sexAndAge = age;
        if (sex == 1) {
            sexAndAge = sexAndAge | 128;
        }

        int weightInt = (int) (weight * 10);
        int weight1 = weightInt & 255;
        int weight2 = (weightInt >> 8) & 0xff;

        int resistance1 = 255;
        int resistance2 = 255;

        if (impedance >= 200 && impedance <= 1500) {
            resistance1 = ((int) impedance) & 0xff;
            resistance2 = (((int) impedance) >> 8) & 0xff;
        }


        if (height < 100) {
            height = 100;
        } else if (height > 220) {
            height = 220;
        }
        int height1 = height & 255;
        int height2 = (height >> 8) & 0xff;
        byte heightByte = (byte) height;


        //0普通1运动员
        // roleType 0普通 1专业运动员 2业余运动员
//        byte roleTypeByte = 0x01;
//        if (roleType == 1) {
//            roleTypeByte = 0x02;
//        }else if(roleType == 2){
//            roleTypeByte = 0x03;
//        }

        byte roleTypeByte = 0x01;//01普通 02运动员
        if (roleType == 1) {//0普通1运动员
            roleTypeByte = 0x02;
        }

        byte[] bytes;
        if ("Body Fat-B1".equals(currentConnectScaleBlueName)
                ||"Body Fat-B2".equals(currentConnectScaleBlueName)
                ||"Body Fat-B16".equals(currentConnectScaleBlueName)
                || Dj8Constant.Dj8_NAME.equals(currentConnectScaleBlueName)){

            LogUtils.i(TAG,"蓝牙用户下发1 currentConnectScaleBlueName = "+currentConnectScaleBlueName);
            bytes = new byte[]{
                    (byte) 0xab,
                    (byte) 0x0e,
                    (byte) 0x99,
                    (byte) Integer.parseInt(btIdArr[0], 16),
                    (byte) Integer.parseInt(btIdArr[1], 16),
                    (byte) Integer.parseInt(btIdArr[2], 16),
                    (byte) Integer.parseInt(btIdArr[3], 16),
                    (byte) Integer.parseInt(btIdArr[4], 16),
                    (byte) Integer.parseInt(btIdArr[5], 16),
                    (byte) Integer.parseInt(btIdArr[6], 16),
                    (byte) sexAndAge,
                    (byte) heightByte,
                    roleTypeByte,
                    (byte) weight1,
                    (byte) weight2,
                    (byte) resistance1,
                    (byte) resistance2
            };

        }else {
            LogUtils.i(TAG,"蓝牙用户下发2 currentConnectScaleBlueName = "+currentConnectScaleBlueName);
            bytes = new byte[]{
                    (byte) 0xab,
                    (byte) 0x0e,
                    (byte) 0x99,
                    (byte) Integer.parseInt(btIdArr[0], 16),
                    (byte) Integer.parseInt(btIdArr[1], 16),
                    (byte) Integer.parseInt(btIdArr[2], 16),
                    (byte) Integer.parseInt(btIdArr[3], 16),
                    (byte) Integer.parseInt(btIdArr[4], 16),
                    (byte) Integer.parseInt(btIdArr[5], 16),
                    (byte) Integer.parseInt(btIdArr[6], 16),
                    (byte) sexAndAge,
                    (byte) height1,
                    (byte) height2,
                    (byte) weight1,
                    (byte) weight2,
                    (byte) resistance1,
                    (byte) resistance2
            };
        }

        LogUtils.i(TAG,"蓝牙用户下发 "+ BytesUtils.bytes2HexStr(bytes));


        return bytes;
    }

    //实际上就是单例模式
    public static ScaleUser getUser() {
        if (mUser == null) {
            synchronized (ScaleUser.class) {
                if (mUser == null) mUser = new ScaleUser();
            }
        }
        return mUser;
    }

    //将用户信息保存
    public static void setModeUserInfo(int id, String huid, String userName, String userHeight, String userWeight,
                                       float impedance, int userSex, int userAge, String birthday, Bitmap userIcon,
                                       float target, String targetTime, String registTime) {
        mUser.setAge(userAge);
        mUser.setNike(userName);
        mUser.setRegistTime(registTime);
        if (userHeight != null)
            mUser.setHeight(Integer.parseInt(userHeight.split(" ")[0]));
        mUser.setSex(userSex);
        if (userWeight != null && !userWeight.equals(""))
            mUser.setWeight(Float.parseFloat(userWeight));
        mUser.setUid(id);
        mUser.setHuId(huid);
        if (birthday != null)
            mUser.setBirthday(birthday);
        mUser.setImpedance(impedance);
        mUser.setIcon(userIcon);
        mUser.setTarget(target);
        mUser.setTargetTime(targetTime);
    }


    public void clearData() {
        mUser.setAge(0);
        mUser.setNike(null);
        mUser.setRegistTime(null);
        mUser.setHeight(0);
        mUser.setSex(-1);
        mUser.setWeight(-1);
        mUser.setUid(-1);
        mUser.setHuId(null);
        mUser.setBirthday(null);
        mUser.setImpedance(0);
        mUser.setIcon(null);
        mUser.setTarget(0);
        mUser.setTargetTime(null);
    }

    @Override
    public String toString() {
        return "ScaleUser{" +
                "mac='" + mac + '\'' +
                ", uid=" + uid +
                ", age=" + age +
                ", birthday='" + birthday + '\'' +
                ", height=" + height +
                ", weight=" + weight +
                ", sex=" + sex +
                ", icon=" + icon +
                ", targetTime='" + targetTime + '\'' +
                ", target=" + target +
                ", registTime='" + registTime + '\'' +
                ", impedance=" + impedance +
                ", huId='" + huId + '\'' +
                ", btId='" + btId + '\'' +
                ", imgUrl='" + imgUrl + '\'' +
                ", nike='" + nike + '\'' +
                '}';
    }

    /**
     * 当前用户是否为访客
     */
    public boolean isLoginPattren() {
        boolean isLoginPattren = false;
        String[] btIdArr = null;
        if (btId != null) {
            btIdArr = btId.split(":");
            if (btIdArr != null && btIdArr.length > 0) {
                int i = 0;
                for (String single : btIdArr) {
                    if ("0".equals(single)) {
                        i++;
                    }
                }
                if (i == btIdArr.length) {
                    isLoginPattren = true;
                }
            }
        }
        return isLoginPattren;
    }

    /**
     * 转换成btId的方法
     *
     * @param s 要求传入参数中为十六进制字符，即0到f，否则返回null
     */
    public synchronized static String toBtId(String s) {
        if (s == null || s.isEmpty()) {
            return null;
        }
        StringBuilder sb = new StringBuilder(s);
        int remainder = s.length() % 7;
        String newStr = "";
        if (remainder > 0) {
            for (int i = 0; i < 7 - remainder; i++) {
                sb.append("a");
            }
            newStr = sb.toString();
        } else {
            newStr = s;
        }
        sb.setLength(0);
        int multiple = newStr.length() / 7;
        int i = 0;
        while (i * multiple < newStr.length()) {
            //如果单个字节字符串，无法转成16进制数，则返回null
            String singleByte = newStr.substring(i * multiple, (i + 1) * multiple);
            try {
                int aaa = Integer.parseInt(singleByte, 16);
                if (aaa > 255) {
                    return null;
                }
            } catch (Exception e) {
                return null;
            }

            sb.append(singleByte).append(":");
            i++;
        }
        if (sb.length() > 1) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }


    //用户id转倍泰id
    public static String userid2btid(long user_id) {
        if (user_id < 0) {
            user_id = 0;
        }
        String userid = user_id + "";
        if (userid.length() > 14) {
            userid = userid.substring(userid.length() - 14, userid.length());
        }
        return userid;
    }






}